package com.zhihuishu.micro.course.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhihuishu.micro.course.service.IBaseService;
import com.zhihuishu.toolkit.jedis.template.JedisExecutor;

public abstract class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Resource
    protected JedisExecutor jedisExecutor;
    /**
     * 由于子类实现该方法 返回具体的mapper的实现类
     * 
     * @return
     */
    protected Logger logger=LoggerFactory.getLogger(getClass());
    public abstract Mapper<T> getMapper();

    @Override
    public T queryById(Integer id) {
        return getMapper().selectByPrimaryKey(id);
    }

    @Override
    public List<T> queryAll() {
        return getMapper().select(null);
    }

    @Override
    public List<T> queryByWhere(T t) {
        return getMapper().select(t);
    }

    @Override
    public T queryOne(T t) {
        return getMapper().selectOne(t);
    }

    @Override
    public PageInfo<T> queryPageListByWhere(T t, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);// 设置分页参数
        List<T> list = getMapper().select(t);// 查询数据
        return new PageInfo<T>(list);
    }

    @Override
    public Integer save(T t) {


        return getMapper().insert(t);
    }

    @Override
    public Integer saveSelective(T t) {

        return getMapper().insertSelective(t);
    }

    @Override
    public Integer update(T t) {

        return getMapper().updateByPrimaryKey(t);
    }

    @Override
    public Integer updateSelective(T t) {

        return getMapper().updateByPrimaryKeySelective(t);
    }

    @Override
    public Integer deleteById(Object id) {
        return getMapper().deleteByPrimaryKey(id);

    }

    @Override
    public Integer deleteByIds(List<Object> ids, String property, Class<T> clazz) {
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, ids);
        return getMapper().deleteByExample(example);
    }
}
